package com.xf.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import com.xf.common.core.entity.FebsAuthUser;
import com.xf.common.core.enums.IsDelEnum;
import com.xf.common.core.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description 默认字段填充
 * @Author FengYi
 * @Date 2020-04-08 14:59
 */
@Slf4j
@Component
public class MybatisFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        FebsAuthUser user = SecurityUtils.getUser();
        this.setFieldValByName("createdBy", user.getUserName(), metaObject);
        this.setFieldValByName("updatedBy", user.getUserName(), metaObject);

        this.setFieldValByName("createdDate", new Date(), metaObject);
        this.setFieldValByName("updatedDate", new Date(), metaObject);
        this.setFieldValByName("status", IsDelEnum.NORMAL.getCode(), metaObject);
        this.setFieldValByName("dataVersion", 0L, metaObject);
        this.setFieldValByName("concurrentVersion", 0L, metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        FebsAuthUser user = SecurityUtils.getUser();
        this.setFieldValByName("updatedBy", user.getUserId(), metaObject);

        this.setFieldValByName("updatedDate", new Date(), metaObject);
    }


}
